package com.lanou.horo.zhuizhui.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.horo.zhuizhui.activity.DetailsActivity;
import com.lanou.horo.zhuizhui.entity.CommunityPageEssenceItem;
import com.lanou.horo.zhuizhui.utility.clickInterface.EveryDayClick;
import com.lanou.horo.zhuizhui.utility.imageLoader.AsyncImageLoader;
import com.lanou.horo.zhuizhui.utility.customImage.RoundNetWorkImageView;
import com.lanou.horo.zhuizhui.utility.imageLoader.VolleySingleton;
import com.lanou.horo.zhuizhui.R;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by dllo on 15/10/26.
 */
public class CommunityPEFAdapter extends
        RecyclerView.Adapter<CommunityPEFAdapter.CommunityPEFViewHolder> {
    private List<CommunityPageEssenceItem.TopicsEntity> entityList;
    private Context context;
//    private AsyncImageLoader asyncImageLoader;
    private ImageLoader imageLoaderVolley;
    private EveryDayClick everyDayClick;


    public List<CommunityPageEssenceItem.TopicsEntity> getEntityList() {
        return entityList;
    }

    public void clickItemView(EveryDayClick click){
        this.everyDayClick = click;
    }


    public CommunityPEFAdapter(Context context) {
        this.context = context;
        imageLoaderVolley = VolleySingleton.getInstance().getImageLoader();
//        asyncImageLoader = new AsyncImageLoader(context);
    }

    public void addTopList(List<CommunityPageEssenceItem.TopicsEntity> datas) {
        entityList = datas;
        notifyDataSetChanged();
    }

    @Override
    public CommunityPEFViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.fragment_page_community_content, null);
        return new CommunityPEFViewHolder(view);
    }

    @Override
    public void onBindViewHolder(CommunityPEFViewHolder holder, int position) {
        if (entityList != null && entityList.size() > 0) {
            holder.itemPosition = position;
            CommunityPageEssenceItem.TopicsEntity entity = entityList.get(position);
            holder.userName.setText(entity.getUser_info().getName());
            holder.topicsTitle.setText(entity.getTitle());
            holder.topicsContent.setText(entity.getContent().get(0).getContent());
            holder.praiseZNum.setText(entity.getUp_count());
            holder.praiseNum.setText(entity.getComment_count());
            String uurl = entity.getUser_info().getAvatar();
            holder.userAvatar.setImageUrl(uurl, imageLoaderVolley);
            String time = entity.getCreated_at();
            holder.postedTime.setText(time);
            if (entity.getContent().size() > 1) {
                String furl = entity.getContent().get(1).getUrl();
                //setBitmap(furl,holder.contentfImageView);
                holder.contentfImageView.setImageUrl(furl, imageLoaderVolley);
                holder.contentsImageView.setImageResource(R.color.white);
                holder.contenttImageView.setImageResource(R.color.white);
                if (entity.getContent().size() > 2) {
                    String surl = entity.getContent().get(2).getUrl();
                   // setBitmap(surl,holder.contentsImageView);
                    holder.contentsImageView.setImageUrl(surl, imageLoaderVolley);
                    holder.contenttImageView.setImageResource(R.color.white);
                    if (entity.getContent().size() > 3) {
                        String turl = entity.getContent().get(3).getUrl();
                        //setBitmap(turl,holder.contenttImageView);
                        holder.contenttImageView.setImageUrl(turl, imageLoaderVolley);
                    }
                }
            }
        }
    }

    @Override
    public int getItemCount() {
        return entityList != null && entityList.size() > 0 ? entityList.size() : 0;
    }

    public class CommunityPEFViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private RoundNetWorkImageView userAvatar;
        private NetworkImageView topicTypesInfo, contentfImageView, contentsImageView, contenttImageView;
        private TextView userName, postedTime, topicsTitle, praiseZNum, praiseNum, topicsContent;
        private int itemPosition;
        public CommunityPEFViewHolder(View itemView) {
            super(itemView);
            userAvatar = (RoundNetWorkImageView) itemView.findViewById(R.id.userAvatar);
            topicTypesInfo = (NetworkImageView) itemView.findViewById(R.id.topicTypesInfo);
            contentfImageView = (NetworkImageView) itemView.findViewById(R.id.contentfImageView);
            contentsImageView = (NetworkImageView) itemView.findViewById(R.id.contentsImageView);
            contenttImageView = (NetworkImageView) itemView.findViewById(R.id.contenttImageView);
            userName = (TextView) itemView.findViewById(R.id.userName);
            postedTime = (TextView) itemView.findViewById(R.id.postedTime);
            topicsTitle = (TextView) itemView.findViewById(R.id.topicsTitle);
            praiseZNum = (TextView) itemView.findViewById(R.id.praiseZNum);
            praiseNum = (TextView) itemView.findViewById(R.id.praiseNum);
            topicsContent = (TextView) itemView.findViewById(R.id.topicsContent);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
           if (entityList != null){
               CommunityPageEssenceItem.TopicsEntity top = entityList.get(itemPosition);
               if (everyDayClick != null){
                   everyDayClick.communityItemClick(top);
               }
           }
        }
    }
//    private void setBitmap(String url, ImageView imageView) {
//        // 给 ImageView 设置一个 tag
//        imageView.setTag(url);
//        // 预设一个图片
//        imageView.setImageResource(R.mipmap.loading);
//        if (!TextUtils.isEmpty(url)) {
//            Bitmap bitmap = asyncImageLoader.loadImage(imageView, url);
//            if (bitmap != null) {
//                imageView.setImageBitmap(bitmap);
//            }
//        }
//    }
}
